﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FbKPI.Model;
using FbKPI.Facade;
using FbKPI.Facade.Validator;
using System.Web.Script.Serialization;
using System.Collections.ObjectModel;
using System.Web.Script.Services;
using System.Transactions;

namespace FBKPI.ProcessRequest
{
    /// <summary>
    /// Summary description for KPIAssignHandler
    /// </summary>
    public class KPIAssignHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            string msg = this.Save();
            context.Response.Write(msg);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private string Save()
        {
            string msg = "OK";
            string action = HttpContext.Current.Request.QueryString["action"];
            int ID = Convert.ToInt16(HttpContext.Current.Request.QueryString["ID"]);
            int RGMID = HttpContext.Current.Request.QueryString["RGM"] == string.Empty ? 0 : Convert.ToInt32(HttpContext.Current.Request.QueryString["RGM"]);
            int LocationID = HttpContext.Current.Request.QueryString["Location"]== string.Empty ? 0 : Convert.ToInt32(HttpContext.Current.Request.QueryString["Location"]);
            int SubBranchID = HttpContext.Current.Request.QueryString["SubBranch"] == string.Empty ? 0 : Convert.ToInt32(HttpContext.Current.Request.QueryString["SubBranch"]);
            bool isActive = Convert.ToBoolean(HttpContext.Current.Request.QueryString["isActive"]);
            bool weightEnable = Convert.ToBoolean(HttpContext.Current.Request.QueryString["weightEnable"]);
            bool uomEnable = Convert.ToBoolean(HttpContext.Current.Request.QueryString["uomEnable"]);
            int UOMID = HttpContext.Current.Request.QueryString["UOM"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["UOM"]);
            int UOMMaxID = HttpContext.Current.Request.QueryString["UOMMax"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["UOMMax"]);
            int UOMMinID = HttpContext.Current.Request.QueryString["UOMMin"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["UOMMin"]);
            int KPIID = HttpContext.Current.Request.QueryString["KPI"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["KPI"]);
            int Verifier = HttpContext.Current.Request.QueryString["Verifier"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["Verifier"]);
            int Weight = HttpContext.Current.Request.QueryString["Weight"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["Weight"]);            
            string Polarization = HttpContext.Current.Request.QueryString["Polarization"];
            int ManualCalculation = HttpContext.Current.Request.QueryString["ManualCalculation"] == string.Empty ? 0 : Convert.ToInt32(HttpContext.Current.Request.QueryString["ManualCalculation"]);
            Employee CurrUser = new EmployeeFacade().GetEmployeeByID(Convert.ToInt32(HttpContext.Current.Request.QueryString["CurrUser"]));

            KPIAssignFacade df = new KPIAssignFacade();
            KPIAssign KPIs = df.GetKPIAssignByID(ID);
            KPIAssignValidator dv = new KPIAssignValidator();

            if (KPIs == null)
            {
                KPIs = new KPIAssign();
                KPIs.ID = ID;
                KPIs.RGM = new RGMFacade().GetRGMByID(RGMID);
                KPIs.Location = new LocationFacade().GetLocationByID(LocationID);
                KPIs.SubBranch = new SubBranchFacade().GetSubBranchByID(SubBranchID);
                KPIs.isActive = isActive;
                KPIs.WeightEnableFlag = weightEnable;
                KPIs.UoMEnableFlag = uomEnable;
                KPIs.UoM = new UoMFacade().GetUoMByID(UOMID);
                KPIs.UoMDetailMax = new UoMDetailFacade().GetUoMDetailByID(UOMMaxID);
                KPIs.UoMDetailMin = new UoMDetailFacade().GetUoMDetailByID(UOMMinID);
                KPIs.KPI = new KPIFacade().GetKPIByID(KPIID);
                KPIs.Verifier = new EmployeeFacade().GetEmployeeByID(Verifier);
                KPIs.Weight = Weight;
                KPIs.Polarization = Polarization;
                KPIs.ManualCalculation = ManualCalculation;
                KPIs.CreateBy = CurrUser.Email;
                msg = dv.ValidateOnSave(KPIs);
            }
            else if (action != "delete")
            {
                KPIs.RGM = new RGMFacade().GetRGMByID(RGMID);
                KPIs.Location = new LocationFacade().GetLocationByID(LocationID);
                KPIs.SubBranch = new SubBranchFacade().GetSubBranchByID(SubBranchID);
                KPIs.isActive = isActive;
                KPIs.WeightEnableFlag = weightEnable;
                KPIs.UoMEnableFlag = uomEnable;
                KPIs.UoM = new UoMFacade().GetUoMByID(UOMID);
                KPIs.UoMDetailMax = new UoMDetailFacade().GetUoMDetailByID(UOMMaxID);
                KPIs.UoMDetailMin = new UoMDetailFacade().GetUoMDetailByID(UOMMinID);
                KPIs.KPI = new KPIFacade().GetKPIByID(KPIID);
                KPIs.Verifier = new EmployeeFacade().GetEmployeeByID(Verifier);
                KPIs.Weight = Weight;
                KPIs.Polarization = Polarization;
                KPIs.ManualCalculation = ManualCalculation;
                KPIs.LastModifiedBy = CurrUser.Email;
                msg = dv.ValidateOnUpdate(KPIs);
            }
            
            else if (action == "delete")
                KPIs.LastModifiedBy = CurrUser.Email;

            if (msg == "OK")
            {
                using (TransactionScope trans = new TransactionScope(TransactionScopeOption.RequiresNew))
                {
                    switch (action)
                    {
                        case "save":
                            msg = df.Save(KPIs);
                            break;
                        case "delete":
                            msg = df.Delete(KPIs);
                            break;
                    }
                    trans.Complete();
                    trans.Dispose();
                }
            }

            return msg;
        }
    }
}